package com.api.gbhg.daiyanlu.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.SixteenDao;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import com.api.gbhg.luoao.utils.PageEntity;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 海关医疗保险缴费经费
 * Mr.dyl
 */
public class SixteenActionApi {
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    @POST
    @Path("/getSixteen")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getSixteen(Map<String,String> param) {
        //String status =  param.get("status");//状态
        String requestId = param.get("requestId");
        //Integer pageNum = Integer.valueOf(param.get("pageNum"));//页数
        //Integer pageSize = Integer.valueOf(param.get("pageSize"));//显示数量
        SixteenDao dao = new SixteenDao();
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
       // page.setPageNumber(pageNum);
        //page.setpageSize(pageSize);
        int total = dao.getMainTableCount(requestId);
        page.setTotal(total);//数据总条数
        //page.setPages((total + pageSize - 1) / pageSize);
        Map<String, Object>[] map = new HashMap[mainTable.getArray().size()];
        int i = 0;
        while (mainTable.next()) {
            map[i] = new HashMap<>();
            map[i].put("lcid",mainTable.getInt("lcid"));//流程id
            map[i].put("xmmin",mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String loginid = jbr.getString("loginid");
            if(loginid==""){
                loginid="系统管理员";
            }

            map[i].put("lcjlsqjlid",mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("jbr",loginid);  //编报人
            BigDecimal hwgz = new BigDecimal("0.00");
            BigDecimal gc = new BigDecimal("0.00");
            BigDecimal fw = new BigDecimal("0.00");
            BigDecimal xecg = new BigDecimal("0.00");
            if(!mainTable.getString("hwgz").equals("")){
                hwgz = hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")));
            }
            if(!mainTable.getString("gc").equals("")){
                gc = gc.add(new BigDecimal(mainTable.getDouble("gc")));
            }
            if(!mainTable.getString("fw").equals("")){
                fw = fw.add(new BigDecimal(mainTable.getDouble("fw")));
            }
            if(!mainTable.getString("xecg").equals("")){
                xecg = xecg.add(new BigDecimal(mainTable.getDouble("xecg")));
            }
            map[i].put("zfcgyswy",hwgz.add(gc).add(fw).add(xecg).toString());//政府采购预算
            map[i].put("hwgz", hwgz.toString());//货物购置
            map[i].put("gc", gc.toString());//修缮工程
            map[i].put("fw",fw.toString());//服务预算
            map[i].put("xecg", xecg.toString());//小额采购
            RecordSet rs= dao.getDetailedTable(mainTable.getString("requestid"),1);
            BigDecimal zxys = new BigDecimal("0.00"); //专项预算
            BigDecimal fzxys = new BigDecimal("0.00");//非专项预算
            BigDecimal je = new BigDecimal("0.00");//金额

            //金额
            while (rs.next()){
                if (rs.getString("sfzxys").equals("0")){
                    zxys=zxys.add(new BigDecimal(rs.getDouble("jfysy")));
                }else if (rs.getString("sfzxys").equals("1")){
                    fzxys=fzxys.add(new BigDecimal(rs.getDouble("jfysy")));
                }
            }
            map[i].put("zxys",zxys.toString());//小额采购
            map[i].put("fzxys",fzxys.toString());//非专项预算
            map[i].put("je",je);//金额
            map[i].put("nys",mainTable.getString("yszje"));//年预算
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_hgylbxjfjfxqbbb");
        return JSON.toJSONString(page);
    }

    // 海关医疗保险
    public String getSixteen1(String requestId) {
        //String status =  param.get("status");//状态
//        String requestId = param.get("requestId");
        //Integer pageNum = Integer.valueOf(param.get("pageNum"));//页数
        //Integer pageSize = Integer.valueOf(param.get("pageSize"));//显示数量
        SixteenDao dao = new SixteenDao();
        RecordSetTrans mainTable = dao.getMainTable(requestId);//获取主表数据
        PageEntity page = new PageEntity();
        // page.setPageNumber(pageNum);
        //page.setpageSize(pageSize);
        int total = dao.getMainTableCount(requestId);
        page.setTotal(total);//数据总条数
        //page.setPages((total + pageSize - 1) / pageSize);
        Map<String, Object>[] map = new HashMap[mainTable.getArray().size()];
        int i = 0;
        while (mainTable.next()) {
            map[i] = new HashMap<>();
            map[i].put("lcid",mainTable.getInt("lcid"));//流程id
            map[i].put("xmmin",mainTable.getString("xmmin"));//项目名称
            RecordSet sbdw = rmcd.getSBDWById(mainTable.getInt("sbdw"));
            sbdw.next();
            map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME")); // 申报单位
            RecordSet sbks = rmcd.getSBKSById(mainTable.getInt("sbks"));
            sbks.next();
            map[i].put("sbks",sbks.getString("departmentmark"));// 申报科室
            RecordSet jbr = rmcd.getUserNameById(mainTable.getInt("jbr"));
            jbr.next();
            String lastname = jbr.getString("lastname");
            if(lastname==""){
                lastname="系统管理员";
            }

            map[i].put("lcjlsqjlid",mainTable.getInt("lcjlsqjlid"));  //流程记录申请记录id
            map[i].put("jbr",lastname);  //编报人
            BigDecimal hwgz = new BigDecimal("0.00");
            BigDecimal gc = new BigDecimal("0.00");
            BigDecimal fw = new BigDecimal("0.00");
            BigDecimal xecg = new BigDecimal("0.00");
            if(!mainTable.getString("hwgz").equals("")){
                hwgz = hwgz.add(new BigDecimal(mainTable.getDouble("hwgz")));
            }
            if(!mainTable.getString("gc").equals("")){
                gc = gc.add(new BigDecimal(mainTable.getDouble("gc")));
            }
            if(!mainTable.getString("fw").equals("")){
                fw = fw.add(new BigDecimal(mainTable.getDouble("fw")));
            }
            if(!mainTable.getString("xecg").equals("")){
                xecg = xecg.add(new BigDecimal(mainTable.getDouble("xecg")));
            }
            map[i].put("zfcgyswy",hwgz.add(gc).add(fw).add(xecg).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//政府采购预算
            map[i].put("hwgz", hwgz.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//货物购置
            map[i].put("gc", gc.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//修缮工程
            map[i].put("fw",fw.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//服务预算
            map[i].put("xecg", xecg.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//小额采购
            RecordSet rs= dao.getDetailedTable(mainTable.getString("requestid"),1);
            BigDecimal zxys = new BigDecimal("0.00"); //专项预算
            BigDecimal fzxys = new BigDecimal("0.00");//非专项预算
            BigDecimal je = new BigDecimal("0.00");//金额

            //金额
            while (rs.next()){
                if (rs.getString("sfzxys").equals("0")){
                    zxys=zxys.add(new BigDecimal(rs.getDouble("jfysy")));
                }else if (rs.getString("sfzxys").equals("1")){
                    fzxys=fzxys.add(new BigDecimal(rs.getDouble("jfysy")));
                }
            }
            map[i].put("zxys",zxys.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//小额采购
            map[i].put("fzxys",fzxys.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//非专项预算
            map[i].put("je",je.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//金额
            BigDecimal yszje = new BigDecimal(mainTable.getDouble("yszje"));
            map[i].put("nys",yszje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));//年预算
            i++;
        }
        page.setList(map);
        page.setFromtable("uf_hgylbxjfjfxqbbb");
        return JSON.toJSONString(page);
    }
}
